package com.yuye.test.redission.controller;

import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Timer;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
public class RedissionController {

    HashedWheelTimer timer = new HashedWheelTimer(new DefaultThreadFactory("redisson-timer"), (long)500, TimeUnit.MILLISECONDS, 1024, false);

    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(
            1,
            new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-test-%d").daemon(true).build()
    );

    Timer javaTimer = new Timer();

    /**
     * java Timer 调度
     * @return success
     */
    @RequestMapping("javaTimerTest")
    public String javaTimerTest(@RequestParam Integer count)  {

        for (int i = 0; i < count; i++) {
            javaTimer.schedule(new java.util.TimerTask() {
                @Override
                public void run() {
                    System.out.println("javaTimerTest-111");
                    try{
                        Thread.sleep(200);
                    }catch (Exception e){

                    }
                }
            }, 0, 1000);
        }

        return "success";
    }

    /**
     * netty 时间轮
     * @return success
     */
    @RequestMapping("wheelTimerTest")
    public String wheelTimerTest(@RequestParam Integer count)  {
        for (int i = 0; i < count; i++) {
            renewal();
        }

        return "success";
    }

    private void renewal(){
        timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) throws Exception {
                System.out.println("wheelTimerTest-1111");
                try{
                    Thread.sleep(200);
                }catch (Exception e){

                }
                renewal();
            }
        },  1000, TimeUnit.MILLISECONDS);
    }

    /**
     * ScheduledExecutorService 调度
     * @return success
     */
    @RequestMapping("scheduledExecutorServiceTest")
    public String scheduledExecutorServiceTest(@RequestParam Integer count)  {

        for (int i = 0; i < count; i++) {
            executorService.scheduleAtFixedRate(new Runnable() {
                                                    @Override
                                                    public void run() {
                                                        System.out.println("scheduledExecutorServiceTest-111");
                                                        try{
                                                            Thread.sleep(200);
                                                        }catch (Exception e){

                                                        }
                                                    }
                                                },
                    1000,
                    1000,
                    TimeUnit.MILLISECONDS
            );
        }

        return "success";
    }

    public static void main(String[] args) throws InterruptedException {
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(
                1,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-test-%d").daemon(true).build()
        );
        executorService.scheduleAtFixedRate(new Runnable() {
                                                @Override
                                                public void run() {
                                                    System.out.println("scheduledExecutorServiceTest-111");
                                                    try{
                                                        Thread.sleep(200);
                                                    }catch (Exception e){

                                                    }
                                                }
                                            },
                1000,
                1000,
                TimeUnit.MILLISECONDS
        );
        Thread.sleep(100000);
    }

}
